const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//切割从蓝牙接收到的数据并转为10进制
const splitString = str => {
  let arr = []
  let newTring = str.replace(/(.{2})/g, '$1,').split(',')
  newTring.pop()
  for (let i = 0; i < newTring.length; i++) {
    if (i == 3 || i == 9) {
      let x = '0x' + newTring[i] + newTring[i + 1]
      // console.log(x,Number(x).toString(10))
      // arr.splice(i + 1, 1);
      arr.push(Number(x).toString(10))
    } else {
      let x = '0x' + newTring[i]
      arr.push(Number(x).toString(10))
    }
  }
  return arr
}

//解析设备编号
const splitBlueStr = str => {
  let arr = []
  let newTring = str.replace(/(.{2})/g, '$1,').split(',')
  newTring.pop()
  for (let i = 0; i < newTring.length; i++) {
    let x = '0x' + newTring[i]
      arr.push(Number(x).toString(10))
  }
  return arr
}

//获取蓝牙数据赋值给当前页面
const pushArr = arr => {
  let hour = (Math.floor(arr[9]*10/3600)).toString().length == 2 ? Math.floor(arr[9]*10/3600) : '0' + Math.floor(arr[9]*10/3600)
  let minute = (Math.floor(arr[9]*10/60 % 60)).toString().length == 2 ? Math.floor(arr[9]*10/60 % 60) : '0' + Math.floor(arr[9]*10/60 % 60)
  hour = hour == NaN || hour == '0NaN' ? '00' : hour
  let obj = {}
  obj.fanType      = arr[2]    //风扇状态
  obj.anionType    = arr[5]    //负离子状态
  obj.electricType = arr[6]    //充电状态
  obj.electricNum  = arr[8]    //电量值
  obj.hour         = hour      //小时
  obj.minute       = minute    //分钟
  obj.lamp         = arr[11]   //灯光
  return obj
}

//提示
const toast = str => {
  wx.showToast({
    title: str,
    icon: 'none',
    duration: 2000
  })
}

const filter = arr =>{
  let arr2 = arr.filter((item, index) => {
    let temArr = []
    arr.forEach(item2 => temArr.push(item2.name))
    return temArr.indexOf(item.name) == index
  })
}

const onDelete = (arr,str) => {
  return arr.filter(item => item.name !== str);
};

const splitArr = (arr) => {
  let newArr = []
  for(var i = 15 ;i<arr.length;i++){
    newArr.push(arr[i])
  }
  return newArr
}

// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

// 监听30s内能否接受到值
const watchNum = (num) =>{
  setInterval(()=>{
    num++
    if(num == 30){
      return num
    }
  },1000)
}


/*函数节流*/
function throttle(fn, interval) {
  var enterTime = 0;//触发的时间
  var gapTime = interval || 3000 ;//间隔时间，如果interval不传，则默认300ms
  return function() {
    var context = this;
    var backTime = new Date();//第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context,arguments);
      enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}


module.exports = {
  formatTime,splitString,splitBlueStr,pushArr,toast,filter,onDelete,splitArr,ab2hex,watchNum,throttle
}
